package com.example.fashionshoes.controller.user;

import com.example.fashionshoes.exception.BusinessException;
import com.example.fashionshoes.pojo.po.AddressBook;
import com.example.fashionshoes.pojo.po.Result;
import com.example.fashionshoes.service.AddressBookService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
public class AddressBookController {
    @Resource
    private AddressBookService addressBookService;

    /**
     * 新增地址簿
     * @param addressBook 地址簿
     * @return 更新结果
     */
    @PostMapping("/add")
    public Result<Boolean> insert(@RequestBody @Validated AddressBook addressBook){
       boolean success = addressBookService.insert(addressBook);
        return Result.success(success);
    }

    /**
     * 获取地址簿信息
     * @return 地址簿信息
     */
    @GetMapping
    public Result<List<AddressBook>> getAddress(){
        List<AddressBook> addressBookList = addressBookService.getAddress();
        return Result.success(addressBookList);
    }

    /**
     * 修改默认收货地址
     * @return null
     */
    @PostMapping("/update")
    public Result<Object> updateDefaultAddress(@RequestBody Integer addressId){
        try {
            addressBookService.updateDefaultAddress(addressId);
        } catch (BusinessException e) {
            return Result.error("修改失败！");
        }
        return Result.success();
    }
}
